<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use App\Models\traits\Search;
class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable,Search;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        "mobile",
        'department_id',
        "password"
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];
    public $appends = ['recordsCount','departmanagerid'];

    public function getRecordsCountAttribute()
    {
        return $this->records()->count();
    }

    public function getRecordsAttribute()
    {
        return $this->records()->get();
    }

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
    ];

    //
    public function records()
    {
        return $this->hasMany(Record::class);
    }

    public function recordsCount()
    {
        return $this->records()->count();
    }

    public function logs()
    {
        return $this->hasMany(Log::class);
    }

    public function attachments()
    {
        return $this->hasMany(Attachment::class);
    }

//    是否超管
    public function isMaster()
    {
        return $this->id == 1;
    }

//    是哪一个部门的管理员
    public function departments()
    {
        return $this->belongsToMany(Department::class, 'department_admin','user_id','department_id');
    }
    public function getDepartmanageridAttribute(){
        return $this->departments->pluck('id');
    }

//    属于哪一个部门
    public function department()
    {
        return $this->belongsTo(Department::class);
    }
}
